//
//  AppStarPre.m
//  Lmj_iOS
//
//  Created by Lmj_su on 2020/11/6.
//

#import "AppStarPre.h"
#import "LoginViewController.h"
#import "MainViewController.h"
#import "OrderViewController.h"
#import "MineViewController.h"

@interface AppStarPre ()

@property (class, nonatomic, strong) UITabBarController *tab;

@end

@implementation AppStarPre

static UITabBarController *_tab = nil;

+ (UITabBarController *)tab {
    if (_tab == nil) {
        _tab = [[UITabBarController alloc]init];
    }
    return _tab;
}

+ (void)setTab:(UITabBarController *)tab {
    if (tab != _tab) {
        _tab = tab;
    }
}

+ (void)toMain {
    //首页
    MainViewController *mainVC = [[MainViewController alloc] init];
    UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:mainVC];
    [self setupChildViewNavController:mainNav title:@"" imageName:@"ic_main" selectedImageName:@"ic_main_select"];
    
    //商家
    OrderViewController *marketVC = [[OrderViewController alloc] init];
    UINavigationController *orderNav = [[UINavigationController alloc] initWithRootViewController:marketVC];
    [self setupChildViewNavController:orderNav title:@"" imageName:@"ic_order" selectedImageName:@"ic_order_select"];
    
    
    //我的
    MineViewController *mineVC = [[MineViewController alloc] init];
    UINavigationController *mineNav = [[UINavigationController alloc] initWithRootViewController:mineVC];
    [self setupChildViewNavController:mineNav title:@"" imageName:@"ic_mine" selectedImageName:@"ic_mine_select"];

    
    _tab = [[UITabBarController alloc]init];
    _tab.tabBar.tintColor = ColorHex(@"#FFFFFF");
    
    UIImageView *iconImg = [PesoKit createImageViewWithFrame:CGRectMake(0, -27, KScreenWidth, kTabbarHeight-13) imageNamed:@"bg_tabbar"];
    iconImg.userInteractionEnabled = YES;
    iconImg.contentMode =  UIViewContentModeCenter;
    [self.tab.tabBar addSubview:iconImg];
    
    _tab.viewControllers = @[mainNav,orderNav,mineNav];
    [[UIApplication sharedApplication].delegate.window setRootViewController:_tab];
    [_tab.tabBar setBackgroundColor:ColorHex(@"#000000")];
}

+ (void)setupChildViewNavController:(UINavigationController *)childNav title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    childNav.tabBarItem.title = title;
    
    UIImage *nomailImageUI = [UIImage imageNamed:imageName];
    UIImage *nomailImageChange = [nomailImageUI imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childNav.tabBarItem.image = nomailImageChange;
    
    UIImage *selectImageUI = [UIImage imageNamed:selectedImageName];
    UIImage *selectImageChange = [selectImageUI imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childNav.tabBarItem.selectedImage = selectImageChange;
    
    [childNav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: ColorHex(@"#999999"),NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateNormal];
    [childNav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: ColorHex(@"#FFFFFF"),NSFontAttributeName:[UIFont systemFontOfSize:11]}forState:UIControlStateSelected];
}

@end
